package com.yjt.admin.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjt.admin.system.constant.SysCons;
import com.yjt.admin.system.dao.OrgMapper;
import com.yjt.admin.system.entity.Org;
import com.yjt.admin.system.form.OrgForm;
import com.yjt.admin.system.form.SystemFileForm;
import com.yjt.admin.system.param.org.OrgParam;
import com.yjt.admin.system.service.OrgLeaderService;
import com.yjt.admin.system.service.OrgService;
import com.yjt.admin.system.service.OrgSuperLeaderService;
import com.yjt.admin.system.service.SystemFileService;
import com.yjt.admin.system.vo.org.OrgVO;
import com.yjt.common.utils.ComUtil;
import com.yjt.common.utils.TreeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 组织表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2021-10-09
 */
@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements OrgService {


    @Resource
    private OrgMapper orgMapper;

    @Autowired
    private OrgLeaderService orgLeaderService;


    @Autowired
    private OrgSuperLeaderService orgSuperLeaderService;

    @Autowired
    private SystemFileService systemFileService;

    @Value("${nginx.path}")
    private String nginxPath;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrg(OrgForm orgForm) {
        //保存组织基本信息
        Org org = new Org();
        BeanUtils.copyProperties(orgForm, org);
        this.save(org);

        //更新logo文件信息
        SystemFileForm systemFileForm = SystemFileForm.builder().fileId(orgForm.getFileId()).actId(org.getOrgId()).build();
        systemFileService.updateOneFile(systemFileForm);

        //保存集团领导/公司领导/部门领导--可选择人员，岗位并且可多选
        orgLeaderService.addOrgLeader(orgForm.getOrgLeaderIdList(), org.getOrgId());

        //判断是集团，直接返回数据
        if (SysCons.ORG_TYPE[1] == orgForm.getOrgType()) {
            return;
        }

        //增加公司上级领导/部门上级领导 --可选择人员，岗位并且可多选
        orgSuperLeaderService.addOrgSuperLeader(orgForm.getOrgSuperLeaderIdList(), org.getOrgId());
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrg(OrgForm orgForm) {
        //保存组织基本信息
        Org org = new Org();
        BeanUtils.copyProperties(orgForm, org);
        this.updateById(org);

        //更新logo文件信息
        SystemFileForm systemFileForm = SystemFileForm.builder().fileId(orgForm.getFileId()).actId(org.getOrgId()).build();
        systemFileService.updateOneFile(systemFileForm);

        //保存集团领导/公司领导/部门领导--可选择人员，岗位并且可多选
        orgLeaderService.updateOrgLeader(orgForm.getOrgLeaderIdList(), org.getOrgId());

        //判断是集团，直接返回数据
        if (SysCons.ORG_TYPE[1] == orgForm.getOrgType()) {
            return;
        }

        //增加公司上级领导/部门上级领导 --可选择人员，岗位并且可多选
        orgSuperLeaderService.updateOrgSuperLeader(orgForm.getOrgSuperLeaderIdList(), org.getOrgId());

    }

    @Override
    public OrgVO getOneOrg(String orgId) {
        //查询组织信息
        OrgVO orgVO = orgMapper.findOneOrgWithOrgId(orgId);
        orgVO.setNginxPath(nginxPath);

        //查询集团领导/公司领导/部门领导
        orgLeaderService.setOrgLeader(orgVO);

        if (SysCons.ORG_TYPE[1] == orgVO.getOrgType()) {
            //集团直接返回
            return orgVO;
        }

        //查询公司上级领导/部门上级领导
        orgSuperLeaderService.setOrgSuperLeader(orgVO);

        return orgVO;
    }

    @Override
    public IPage<OrgVO> getPageOrg(OrgParam orgParam) {
        return orgMapper.findPageOrg(orgParam.toPage(), orgParam);
    }

    @Override
    public IPage<OrgVO> getTreePageOrg(OrgParam orgParam) {
        long pageSize = orgParam.getPageSize();
        long pageNumber = orgParam.getPageNumber();
        //预留权限接口
        IPage<OrgVO> orgVOIPage = orgParam.toPage();
        List<OrgVO> orgVOList = orgMapper.findAllOrg(orgParam);
        //判断是null直接返回
        if (!ComUtil.isNotNullList(orgVOList)) {
            return orgVOIPage;
        }


        //构造树结构
        orgVOList = TreeUtil.loopRoot(orgVOList);
        //设置返回参数
        orgVOIPage.setTotal(orgVOList.size());
        orgVOList = orgVOList.stream().skip((pageNumber-1L) * pageSize).limit(pageSize).collect(Collectors.toList());
        orgVOIPage.setRecords(orgVOList);
        return orgVOIPage;
    }
}
